﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IDotNet.MapLibs.BMap;
using IDotNet.MapLibs.Model;

namespace TestConsole
{
    public class AddressMap
    {
        private const string City = "深圳";
        static readonly object lockObj = new object();

        public static void Test()
        {
            var sql = "SELECT top 10 Id,Address,Lng,Lat,State FROM dbo.UserAddress WHERE State=2";
            var mapService = new BMapGeocoding();
            var opts = new ParallelOptions()
            {
            };
            while (true)
            {
                try
                {
                    //var ds = LZL.SqlServerHelper.ExecuteDataset(Conn.TestConn, CommandType.Text, sql);
                    var dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(sql, Conn.TestConn);
                    da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        break;
                    }
                    Parallel.ForEach(dt.AsEnumerable().ToList(), opts, p =>
                    {
                        var data = mapService.Geocoder(new BMapGeocoderRequestModel()
                          {
                              address = p[1].ToString(),
                              city = City
                          });
                        lock (lockObj)
                        {
                            if (data.IsSuccess())
                            {
                                p["Lat"] = data.AddressLocaltion.lat;
                                p["Lng"] = data.AddressLocaltion.lng;
                                p["State"] = 1;
                            }
                            else
                            {
                                p["State"] = 22;
                            }
                        }
                        //p.SetModified();
                    });
                    //SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(da);
                    // da.AcceptChangesDuringUpdate = false;
                    da.UpdateCommand = new SqlCommand("UPDATE dbo.UserAddress SET Lat=@Lat,Lng=@Lng,State=@State WHERE Id=@Id");
                    da.UpdateCommand.Parameters.AddRange(new[]
            {
                new SqlParameter("@Lat",SqlDbType.Float,20,"Lat"), 
                new SqlParameter("@Lng",SqlDbType.Float,20,"Lng"), 
                new SqlParameter("@State",SqlDbType.TinyInt,3,"State"), 
                new SqlParameter("@Id",SqlDbType.Int,10,"Id"), 
            });
                    da.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
                    //每次往返处理的行数
                    da.UpdateBatchSize = 10;
                    var result = da.Update(dt);
                    dt.AcceptChanges();
                    Console.WriteLine(DateTime.Now);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Thread.Sleep(2000);
                }
            }

            //SqlServerHelper.ExecuteNonQuery(Conn.TestConn, CommandType.Text, "");
        }



    }
}
